<?php
/**
 * PostController.php
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Blog
 */

/** abstract base controller */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * Blog_PostController
 *
 * post controller
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Blog
 */
class Blog_PostController extends Madoqua_Controller_Action_Abstract {

    /**
     * Post model
     *
     * @var Blog_Post
     */
    private $_postModel;

    /**
     * Comments model
     *
     * @var Blog_Comment
     */
    private $_commentModel;

    /**
     * category model
     *
     * @var Blog_Category
     */
    private $_categoryModel;

    /**
     * initialization
     */
    public function init()
    {
        parent::init();

        $this->_modelLoader->load(array('Blog_Post', 'Blog_Comment', 'Blog_Category'));
        $this->_postModel = new Blog_Post();
        $this->_commentModel = new Blog_Comment();
        $this->_categoryModel = new Blog_Category();
        //set up models
    }

    /**
     * read a post
     */
    public function readAction()
    {
        $slug = $this->_getParam('slug');
        $id = (int) $this->_getParam('id');
        //slug and/or id from the params

        if (!empty($slug)) {
            $post = $this->_postModel->fetchBySlug($slug);
        } elseif (!empty($id)) {
            $post = $this->_postModel->fetchById($id);
        } else {
            throw new Exception('No slug or id');
            //FIXME this should amount to a 404
        }
        //find post using slug or id and throw an exception if both aren't set

        $this->view->post = $post;
    }

    /**
     * list posts
     */
    public function listAction()
    {
        $start = $this->_getParam('start', null);
        if ($start == null) {
            $start = time() - 24*60*60*14;
        }
        $end = $this->_getParam('end', null);
        if ($end == null) {
            $end = time();
        }
        $this->view->start = $start;
        $this->view->end = $end;
        //start and end timestamp
        //FIXME allow for nicer dates in url

        $posts = $this->_postModel->fetchByTimeSpan($start, $end);
        //select all posts in this period
        $this->view->posts = $posts;
    }

    /**
     * generate post overview
     */
    public function overviewAction()
    {
        $count = $this->_getParam('count', null);
        //get count

        $posts = $this->_postModel->fetchAll(null, null, $count);
        //select all posts in this period
        $this->view->posts = $posts;
    }

    /**
     * display recent posts
     */
    public function recentAction()
    {
        $count = (int) $this->_getParam('count', 10);
        //the number of posts to find
        $posts = $this->_postModel->fetchAll(null, 'id DESC', $count);
        //fetch the latest $count posts

        $this->view->posts = $posts;
    }

    /**
     * get a single last post
     */
    public function lastAction()
    {
        $post = $this->_postModel->fetchLatestPost();
        if (!empty($post)) {
            $this->getRequest()->setParam('post', $post);
            $this->view->post = $post;
            //        $this->render('read');
        } else {
            $this->getHelper('viewRenderer')->setNoRender();
        }
    }

    /**
     * fetch previous/next posts for a given post
     */
    public function prevnextAction()
    {
        $post = $this->getPost();
        $count = (int) $this->getRequest()->getParam('count', 1);
        //the number of posts to fetch
        $this->view->previous = $this->_postModel->fetchPreviousPosts($post, $count);
        //the previous posts
        $this->view->next = $this->_postModel->fetchNextPosts($post, $count);
        //the next posts
    }

    /**
     * list categories for a post
     */
    public function categoriesAction()
    {
        $this->view->post = $this->getPost();
        $this->view->categories = $this->_categoryModel->getList();
    }

    /**
     * get Post
     *
     * @return Zend_Db_Table_Row
     * @todo accept id/slug as param?
     */
    private function getPost()
    {
        $post = $this->getRequest()->getParam('post');
        if (!($post instanceof Blog_Post_Row)) {
            throw new Exception('Post param not set');
        }
        //the post we want to work from
        return $post;
    }
}